package com.crm.business.utils;

import com.crm.business.crm.service.ICrmFieldService;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class InitEsIndexRunner implements ApplicationRunner {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        for (CrmEnum value : CrmEnum.values()) {
//            if (!value.equals(CrmEnum.RECEIVABLES_PLAN) && !value.equals(CrmEnum.CUSTOMER_POOL)
//                    && !value.equals(CrmEnum.MARKETING) && !value.equals(CrmEnum.INVOICE)
//                    &&!restTemplate.indexExists(value.getIndex())){
//                SpringUtils.getBean(ICrmFieldService.class).initData(value.getType());
//                log.info("es {} index init success!",value.getIndex());
//            }
//        }
        for (CrmEnum value : CrmEnum.values()) {
            if (CrmEnum.exclude(value)){
                String key = value.getIndex();
                if(!restTemplate.indexExists(key)){
                    log.info("init index --------------------------------------- {} method!",key);
                    SpringUtils.getBean(ICrmFieldService.class).initData(value.getType());
                    log.info("es {} index init success!",key);
                }
            }
        }
    }
}
